home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-admin / import-mt.php < prev    next >
Encoding:
PHP Script  |  2005-04-19  |  17.2 KB  |  419 lines

  1. <?php
  2. define('MTEXPORT', '');
  3. // enter the relative path of the import.txt file containing the mt entries. If the file is called import.txt and it is /wp-admin, then this line
  4. //should be define('MTEXPORT', 'import.txt');
  5.  
  6. if (!file_exists('../wp-config.php')) die("There doesn't seem to be a wp-config.php file. You must install WordPress before you import any entries.");
  7. require('../wp-config.php');
  8. require ('upgrade-functions.php');
  9. $step = $_GET['step'];
  10. if (!$step) $step = 0;
  11. header( 'Content-Type: text/html; charset=utf-8' );
  12. ?>
  13. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  14. <html xmlns="http://www.w3.org/1999/xhtml">
  15. <title>WordPress › Import from Movable Type</title>
  16. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  17. <style media="screen" type="text/css">
  18.     body {
  19.         font-family: Georgia, "Times New Roman", Times, serif;
  20.         margin-left: 20%;
  21.         margin-right: 20%;
  22.     }
  23.     #logo {
  24.         margin: 0;
  25.         padding: 0;
  26.         background-image: url(http://wordpress.org/images/logo.png);
  27.         background-repeat: no-repeat;
  28.         height: 60px;
  29.         border-bottom: 4px solid #333;
  30.     }
  31.     #logo a {
  32.         display: block;
  33.         text-decoration: none;
  34.         text-indent: -100em;
  35.         height: 60px;
  36.     }
  37.     p {
  38.         line-height: 140%;
  39.     }
  40.     #authors li     {
  41.         padding:3px;
  42.         border: 1px solid #ccc;
  43.         width: 40%;
  44.         margin-bottom:2px;
  45.     }
  46.     </style>
  47. </head><body> 
  48. <h1 id="logo"><a href="http://wordpress.org">WordPress</a></h1> 
  49. <?php
  50. switch($step) {
  51.  
  52.     case 0:
  53. ?> 
  54. <p>Howdy! We’re about to begin the process to import all of your Movable Type entries into WordPress. Before we get started, you need to edit this file (<code>import-mt.php</code>) and change one line so we know where to find your MT export file. To make this easy put the import file into the <code>wp-admin</code> directory. Look for the line that says:</p>
  55. <p><code>define('MTEXPORT', '');</code></p>
  56. <p>and change it to</p>
  57. <p><code>define('MTEXPORT', 'import.txt');</code></p>
  58. <p>You have to do this manually for security reasons.</p>
  59. <p>If you've done that and you’re all ready, <a href="import-mt.php?step=1">let's go</a>! Remember that the import process may take a minute or so if you have a large number of entries and comments. Think of all the rebuilding time you'll be saving once it's done. :)</p>
  60. <p>The importer is smart enough not to import duplicates, so you can run this multiple times without worry if—for whatever reason—it doesn't finish. If you get an <strong>out of memory</strong> error try splitting up the import file into pieces. </p>
  61. <?php
  62.     break;
  63.     
  64.     case 1:
  65. if ('' != MTEXPORT && !file_exists(MTEXPORT)) die("The file you specified does not seem to exist. Please check the path you've given.");
  66. if ('' == MTEXPORT) die("You must edit the MTEXPORT line as described on the <a href='import-mt.php'>previous page</a> to continue.");
  67. // Bring in the data
  68. set_magic_quotes_runtime(0);
  69. $importdata = file(MTEXPORT); // Read the file into an array
  70. $importdata = implode('', $importdata); // squish it
  71. $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
  72. $importdata = preg_replace("/\n--------\n/", "--MT-ENTRY--\n", $importdata);
  73. $authors = array();
  74. $temp = array();
  75. $posts = explode("--MT-ENTRY--", $importdata);
  76. unset( $importdata ); // Free up memory
  77.  
  78. function users_form($n) {
  79.     global $wpdb, $testing;
  80.     $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
  81.     ?><select name="userselect[<?php echo $n; ?>]">
  82.     <option value="#NONE#">- Select -</option>
  83.     <?php foreach($users as $user) {
  84.         echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
  85.         } ?>
  86.     </select>
  87. <?php }
  88.  
  89. $i = -1;
  90. foreach ($posts as $post) { 
  91.     if ('' != trim($post)) {
  92.         ++$i;
  93.         unset($post_categories);
  94.         preg_match("|AUTHOR:(.*)|", $post, $thematch);
  95.         $thematch = trim($thematch[1]);
  96.         array_push($temp,"$thematch"); //store the extracted author names in a temporary array
  97.         }
  98.     }//end of foreach
  99. //we need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting.
  100. $authors[0] = array_shift($temp); 
  101. $y = count($temp) + 1;
  102. for ($x = 1; $x < $y; $x++) {
  103.     $next = array_shift($temp);
  104.     if (!(in_array($next,$authors))) array_push($authors, "$next");
  105.     }
  106. //by this point, we have all unique authors in the array $authors
  107. ?><p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as <code>admin</code>s entries.'); ?></p>
  108. <p><?php _e('Below, you can see the names of the authors of the MovableType posts in <i>italics</i>. For each of these names, you can either pick an author in your WordPress installation from the menu, or enter a name for the author in the textbox.'); ?></p>
  109. <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
  110.     <?php
  111.     echo '<ol id="authors">';
  112.     echo '<form action="?step=2" method="post">';
  113.     $j = -1;
  114.     foreach ($authors as $author) {
  115.     ++$j;
  116.     echo '<li><i>'.$author.'</i><br />'.'<input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30">';
  117.     users_form($j);
  118.     echo '</li>';
  119.     }
  120.     echo '<input type="submit" value="Submit">'.'<br/>';
  121.     echo '</form>';
  122.     echo '</ol>';
  123.     
  124.     flush();
  125.  
  126.     break;
  127.     
  128.     case 2:
  129.     $newauthornames = array();
  130.     $formnames = array();
  131.     $selectnames = array();
  132.     $mtnames = array();
  133.     foreach($_POST['user'] as $key => $line) { 
  134.     $newname = trim(stripslashes($line)); 
  135.     if ($newname == '') $newname = 'left_blank';//passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form.
  136.     array_push($formnames,"$newname");
  137.     }// $formnames is the array with the form entered names
  138.     foreach ($_POST['userselect'] as $user => $key) {
  139.     $selected = trim(stripslashes($key));
  140.     array_push($selectnames,"$selected");
  141.     }
  142.     $count = count($formnames);
  143.     for ($i = 0; $i < $count; $i++) {
  144.     if ( $selectnames[$i] != '#NONE#') {//if no name was selected from the select menu, use the name entered in the form
  145.     array_push($newauthornames,"$selectnames[$i]");
  146.     } 
  147.     else {
  148.     array_push($newauthornames,"$formnames[$i]");
  149.     }
  150.     }
  151.  
  152.     $j = -1;
  153.     //function to check the authorname and do the mapping
  154.     function checkauthor($author) {
  155.     global $wpdb, $mtnames, $newauthornames, $j;//mtnames is an array with the names in the mt import file
  156.     $md5pass = md5(changeme);
  157.     if (!(in_array($author, $mtnames))) { //a new mt author name is found
  158.         ++$j;
  159.         $mtnames[$j] = $author; //add that new mt author name to an array 
  160.         $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$newauthornames[$j]'"); //check if the new author name defined by the user is a pre-existing wp user
  161.         if (!$user_id) { //banging my head against the desk now. 
  162.             if ($newauthornames[$j] == 'left_blank') { //check if the user does not want to change the authorname
  163.                 $wpdb->query("INSERT INTO $wpdb->users (user_level, user_login, user_pass, user_nickname) VALUES ('1', '$author', '$md5pass', '$author')"); // if user does not want to change, insert the authorname $author
  164.                 $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$author'");
  165.                 $newauthornames[$j] = $author; //now we have a name, in the place of left_blank.
  166.             } else {
  167.             $wpdb->query("INSERT INTO $wpdb->users (user_level, user_login, user_pass, user_nickname) VALUES ('1', '$newauthornames[$j]', '$md5pass', '$newauthornames[$j]')"); //if not left_blank, insert the user specified name
  168.             $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$newauthornames[$j]'");
  169.             }
  170.         } else return $user_id; // return pre-existing wp username if it exists
  171.     } else {
  172.     $key = array_search($author, $mtnames); //find the array key for $author in the $mtnames array
  173.     $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$newauthornames[$key]'");//use that key to get the value of the author's name from $newauthornames
  174.     }
  175.     return $user_id;
  176. }//function checkauthor ends here
  177.  
  178.     //bring in the posts now
  179. set_magic_quotes_runtime(0);
  180. $importdata = file(MTEXPORT); // Read the file into an array
  181. $importdata = implode('', $importdata); // squish it
  182. $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
  183. $importdata = preg_replace("/\n--------\n/", "--MT-ENTRY--", $importdata);
  184. $authors = array();
  185. $temp = array();
  186. $posts = explode("--MT-ENTRY--", $importdata);
  187. unset( $importdata ); // Free up memory
  188.  
  189. $i = -1;
  190. echo "<ol>";
  191. foreach ($posts as $post) { if ('' != trim($post)) {
  192.     ++$i;
  193.     unset($post_categories);
  194.     echo "<li>Processing post... ";
  195.  
  196.     // Take the pings out first
  197.     preg_match("|(-----\n\nPING:.*)|s", $post, $pings);
  198.     $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post);
  199.  
  200.     // Then take the comments out
  201.     preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments);
  202.     $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post);
  203.     
  204.     // We ignore the keywords
  205.     $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '', $post);
  206.     
  207.     // We want the excerpt
  208.     preg_match("|-----\nEXCERPT:(.*)|s", $post, $excerpt);
  209.     $excerpt = addslashes(trim($excerpt[1]));
  210.     $post = preg_replace("|(-----\nEXCERPT:.*)|s", '', $post);
  211.     
  212.     // We're going to put extended body into main body with a more tag
  213.     preg_match("|-----\nEXTENDED BODY:(.*)|s", $post, $extended);
  214.     $extended = trim($extended[1]);
  215.     if ('' != $extended) $extended = "\n<!--more-->\n$extended";
  216.     $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post);
  217.     
  218.     // Now for the main body
  219.     preg_match("|-----\nBODY:(.*)|s", $post, $body);
  220.     $body = trim($body[1]);
  221.     $post_content = addslashes($body . $extended);
  222.     $post = preg_replace("|(-----\nBODY:.*)|s", '', $post);
  223.     
  224.     // Grab the metadata from what's left
  225.     $metadata = explode("\n", $post);
  226.     foreach ($metadata as $line) {
  227.         preg_match("/^(.*?):(.*)/", $line, $token);
  228.         $key = trim($token[1]);
  229.         $value = trim($token[2]);
  230.         // Now we decide what it is and what to do with it
  231.         switch($key) {
  232.             case '':
  233.                 break;
  234.             case 'AUTHOR':
  235.                 $post_author = $value;
  236.                 break;
  237.             case 'TITLE':
  238.                 $post_title = addslashes($value);
  239.                 echo '<i>'.stripslashes($post_title).'</i>... ';
  240.                 $post_name = sanitize_title($post_title);
  241.                 break;
  242.             case 'STATUS':
  243.                 // "publish" and "draft" enumeration items match up; no change required
  244.                 $post_status = $value;
  245.                 if (empty($post_status)) $post_status = 'publish';
  246.                 break;
  247.             case 'ALLOW COMMENTS':
  248.                 $post_allow_comments = $value;
  249.                 if ($post_allow_comments == 1) {
  250.                     $comment_status = 'open';
  251.                 } else {
  252.                     $comment_status = 'closed';
  253.                 }
  254.                 break;
  255.             case 'CONVERT BREAKS':
  256.                 $post_convert_breaks = $value;
  257.                 break;
  258.             case 'ALLOW PINGS':
  259.                 $post_allow_pings = trim($meta[2][0]);
  260.                 if ($post_allow_pings == 1) {
  261.                     $post_allow_pings = 'open';
  262.                 } else {
  263.                     $post_allow_pings = 'closed';
  264.                 }
  265.                 break;
  266.             case 'PRIMARY CATEGORY':
  267.                 $post_categories[] = addslashes($value);
  268.                 break;
  269.             case 'CATEGORY':    
  270.                 $post_categories[] = addslashes($value);
  271.                 break;
  272.             case 'DATE':
  273.                 $post_date = strtotime($value);
  274.                 $post_date = date('Y-m-d H:i:s', $post_date);
  275.                 $post_date_gmt = get_gmt_from_date("$post_date");
  276.                 break;
  277.             default:
  278. //                echo "\n$key: $value";
  279.                 break;
  280.         } // end switch
  281.     } // End foreach
  282.  
  283.     // Let's check to see if it's in already
  284.     if ($wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$post_title' AND post_date = '$post_date'")) {
  285.         echo "Post already imported.";
  286.     } else {
  287.         $post_author = checkauthor($post_author);//just so that if a post already exists, new users are not created by checkauthor
  288.         $wpdb->query("INSERT INTO $wpdb->posts (
  289.             post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt,  post_status, comment_status, ping_status, post_name, post_modified, post_modified_gmt)
  290.             VALUES 
  291.             ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$excerpt', '$post_status', '$comment_status', '$ping_status', '$post_name','$post_date', '$post_date_gmt')");
  292.         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$post_title' AND post_date = '$post_date'");
  293.         if (0 != count($post_categories)) {
  294.             foreach ($post_categories as $post_category) {
  295.             // See if the category exists yet
  296.             $cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'");
  297.             if (!$cat_id && '' != trim($post_category)) {
  298.                 $cat_nicename = sanitize_title($post_category);
  299.                 $wpdb->query("INSERT INTO $wpdb->categories (cat_name, category_nicename) VALUES ('$post_category', '$cat_nicename')");
  300.                 $cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'");
  301.             }
  302.             if ('' == trim($post_category)) $cat_id = 1;
  303.             // Double check it's not there already
  304.             $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = $cat_id");
  305.  
  306.              if (!$exists) { 
  307.                 $wpdb->query("
  308.                 INSERT INTO $wpdb->post2cat
  309.                 (post_id, category_id)
  310.                 VALUES
  311.                 ($post_id, $cat_id)
  312.                 ");
  313.             }
  314.         } // end category loop
  315.         } else {
  316.             $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = 1");
  317.             if (!$exists) $wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($post_id, 1) ");
  318.         }
  319.         echo " Post imported successfully...";
  320.         // Now for comments
  321.         $comments = explode("-----\nCOMMENT:", $comments[0]);
  322.         foreach ($comments as $comment) {
  323.         if ('' != trim($comment)) {
  324.             // Author
  325.             preg_match("|AUTHOR:(.*)|", $comment, $comment_author);
  326.             $comment_author = addslashes(trim($comment_author[1]));
  327.             $comment = preg_replace('|(\n?AUTHOR:.*)|', '', $comment);
  328.  
  329.             preg_match("|EMAIL:(.*)|", $comment, $comment_email);
  330.             $comment_email = addslashes(trim($comment_email[1]));
  331.             $comment = preg_replace('|(\n?EMAIL:.*)|', '', $comment);
  332.  
  333.             preg_match("|IP:(.*)|", $comment, $comment_ip);
  334.             $comment_ip = trim($comment_ip[1]);
  335.             $comment = preg_replace('|(\n?IP:.*)|', '', $comment);
  336.  
  337.             preg_match("|URL:(.*)|", $comment, $comment_url);
  338.             $comment_url = addslashes(trim($comment_url[1]));
  339.             $comment = preg_replace('|(\n?URL:.*)|', '', $comment);
  340.  
  341.             preg_match("|DATE:(.*)|", $comment, $comment_date);
  342.             $comment_date = trim($comment_date[1]);
  343.             $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
  344.             $comment = preg_replace('|(\n?DATE:.*)|', '', $comment);
  345.  
  346.             $comment_content = addslashes(trim($comment));
  347.             $comment_content = str_replace('-----', '', $comment_content);
  348.  
  349.             // Check if it's already there
  350.             if (!$wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_date = '$comment_date' AND comment_content = '$comment_content'")) {
  351.                 $wpdb->query("INSERT INTO $wpdb->comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_approved)
  352.                 VALUES
  353.                 ($post_id, '$comment_author', '$comment_email', '$comment_url', '$comment_ip', '$comment_date', '$comment_content', '1')");
  354.                 echo " Comment added.";
  355.             }
  356.         }
  357.         }
  358.  
  359.         // Finally the pings
  360.         // fix the double newline on the first one
  361.         $pings[0] = str_replace("-----\n\n", "-----\n", $pings[0]);
  362.         $pings = explode("-----\nPING:", $pings[0]);
  363.         foreach ($pings as $ping) {
  364.         if ('' != trim($ping)) {
  365.             // 'Author'
  366.             preg_match("|BLOG NAME:(.*)|", $ping, $comment_author);
  367.             $comment_author = addslashes(trim($comment_author[1]));
  368.             $ping = preg_replace('|(\n?BLOG NAME:.*)|', '', $ping);
  369.  
  370.             $comment_email = '';
  371.  
  372.             preg_match("|IP:(.*)|", $ping, $comment_ip);
  373.             $comment_ip = trim($comment_ip[1]);
  374.             $ping = preg_replace('|(\n?IP:.*)|', '', $ping);
  375.  
  376.             preg_match("|URL:(.*)|", $ping, $comment_url);
  377.             $comment_url = addslashes(trim($comment_url[1]));
  378.             $ping = preg_replace('|(\n?URL:.*)|', '', $ping);
  379.  
  380.             preg_match("|DATE:(.*)|", $ping, $comment_date);
  381.             $comment_date = trim($comment_date[1]);
  382.             $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
  383.             $ping = preg_replace('|(\n?DATE:.*)|', '', $ping);
  384.       
  385.              preg_match("|TITLE:(.*)|", $ping, $ping_title);
  386.             $ping_title = addslashes(trim($ping_title[1]));
  387.             $ping = preg_replace('|(\n?TITLE:.*)|', '', $ping);
  388.  
  389.             $comment_content = addslashes(trim($ping));
  390.             $comment_content = str_replace('-----', '', $comment_content);
  391.             
  392.             $comment_content = "<strong>$ping_title</strong>\n\n$comment_content";
  393.       
  394.             // Check if it's already there
  395.             if (!$wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_date = '$comment_date' AND comment_content = '$comment_content'")) {
  396.                 $wpdb->query("INSERT INTO $wpdb->comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_approved, comment_type)
  397.                 VALUES
  398.                 ($post_id, '$comment_author', '$comment_email', '$comment_url', '$comment_ip', '$comment_date', '$comment_content', '1', 'trackback')");
  399.                 echo " Comment added.";
  400.             }
  401.  
  402.         }
  403.         }
  404.     }
  405.     echo "</li>";
  406.     flush();
  407.  
  408. } }
  409. upgrade_all();
  410. ?>
  411. </ol>
  412. <h3>All done. <a href="../">Have fun!</a></h3>
  413. <?php
  414.     break;
  415. }
  416. ?> 
  417. </body>
  418. </html>
  419.